//
//  TaskDetailViewController.m
//  Trackit
//
//  Created by yaodaobin on 2017/4/9.
//  Copyright © 2017年 yaodaobin. All rights reserved.
//

#import "TaskDetailViewController.h"
#import "TaskDetailDataSource.h"
#import "TaskDetailModel.h"
#import "LivestockMapViewController.h"

@interface TaskDetailViewController ()

@property (nonatomic, strong) TaskDetailModel *taskDetail;

@end

@implementation TaskDetailViewController
#pragma mark - Controller
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initParams];
    [self initUI];
    [self requestTaskDetail];
}

#pragma mark - Init
- (void)initParams {
    [super initParams];
    self.hideRefreshHeader = YES;
    self.hideRefreshFooter = YES;
}

- (void)initUI {
    [super initUI];
    [self setCustomTitle:NSLocalizedString(@"任务详细", nil)];
    
    self.tableView.estimatedRowHeight = 60;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.tableView.bounds), 20)];
    headerView.backgroundColor = [UIColor clearColor];
    self.tableView.tableFooterView = headerView;
}

#pragma mark - Request
- (void)requestTaskDetail {
    kWeakSelf
    [self showWaitingIcon];
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic safeSetObject:self.taskDetailID forKey:@"taskDetailID"];
    [TrackitDataEngine getTaskDetail:dic onComplete:^(NSDictionary *data, NSError *error) {
        [weakSelf dismissWaitingIcon];
        if (data) {
            weakSelf.taskDetail = [[TaskDetailModel alloc] initWithData:data[@"Data"]];
            [TaskDetailDataSource createTaskDetailDataSource:weakSelf.data model:weakSelf.taskDetail];
            [weakSelf.tableView reloadData];
            UIBarButtonItem *rightBtn = [UIBarButtonItem barItemWithTitle:NSLocalizedString(@"开始任务", nil) target:self action:@selector(startTask)];
            weakSelf.navigationItem.rightBarButtonItem = rightBtn;
        } else {
            [TKUtil showMessage:error.userInfo[@"message"]];
        }
    }];
}

#pragma mark - Private
- (void)startTask  {
    LivestockMapViewController *vc = [[LivestockMapViewController alloc] init];
    vc.taskDetail = self.taskDetail;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - UITableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:NSStringFromClass([UITableViewCell class])];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    TaskDetailDataSource *item = self.data[indexPath.row];
    cell.textLabel.text = item.title;
    cell.detailTextLabel.text = item.content;
    return cell;
}

@end
